
import {baseURL} from './baseUrl.js'
const whiteList = ['miniapp/shop/']
import store from "@/store";

const http = (options) => {
	// console.log(1, options);
    return new Promise((resolve, reject) => {
		const hideLoading = options.hideLoading || false;
		// if(!hideLoading){
		// 	uni.showLoading({
		// 	    title: '加载中...',
		// 	    mask: options.load || false 
		// 	});
		// }
        
		var pass= false;
		for(var i = 0;i<whiteList.length;i++){
			if(options.url.indexOf(whiteList[i]) != -1){
				pass = true;
			}
		}
		const token = uni.getStorageSync('token');
		if(!token && !pass){
			store.dispatch('login').then(res => {
			});
			// uni.hideLoading();
			return
		}
        try{
            uni.request({
                url: (options.baseURL || baseURL) + options.url,
                method: options.method || 'POST', 
                data: options.data, 
                sslVerify:false,
                header: options.method == 'POST' ? {
                 'X-Requested-With': 'XMLHttpRequest',
                 'Content-Type': 'application/json; charset=UTF-8',
				 'token': uni.getStorageSync('token')
                } : {
                 'X-Requested-With': 'XMLHttpRequest',
                 "Accept": "application/json",
                 "Content-Type": "application/json; charset=UTF-8",
				 'token':uni.getStorageSync('token')
                },
                success: res => {
					const data = res.data
					if(data.httpCode==200){
						resolve(data);
					}else{
						uni.showToast({
						    title: data.message,
						    icon: 'error'
						})
					}
					
                },
                fail: (err) => {
                    reject(err);
                    console.log(err);
                   
                    uni.showToast({
                        title: '请检查网络连接',
                        icon: 'none'
                    })
                },
                complete: () => {
                    uni.hideLoading();
                }
            });
        }catch(e){
            uni.hideLoading();
            uni.showToast({
                title: '服务端异常',
                icon: 'none'
            })
        }
       
    })
}

export default http